//
//  DLW_PlayMusic.m
//  27-homework-AVAudioPlay
//
//  Created by 王道林 on 16/8/21.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "DLW_PlayMusic.h"

#import <AVFoundation/AVFoundation.h>

@interface DLW_PlayMusic ()

@property (nonatomic, retain) NSTimer *timer;

@end
@implementation DLW_PlayMusic

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:AVPlayerItemDidPlayToEndTimeNotification];
    [super dealloc];
}
/** 单例. */
+ (instancetype)sharePlayMusic
{
    static DLW_PlayMusic *playMusic = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playMusic = [[DLW_PlayMusic alloc] init];
    });
    return playMusic;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.player = [[AVPlayer alloc] init];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playEndAction) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    }
    return self;
}

- (void)playAction:(NSTimer *)timer
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(musicPlayWithProgress: during:)]) {
        float progress = 1.0 * self.player.currentTime.value / self.player.currentTime.timescale;
        [self.delegate musicPlayWithProgress:progress during:self.during];
    }
}
- (void)playEndAction
{
    if ([self.delegate respondsToSelector:@selector(musicPlayFinished)]) {
        [self.delegate musicPlayFinished];
    }
}
/** 网址 setter 方法*/
- (void)setUrlStr:(NSString *)urlStr
{
    if (!urlStr) {
        return;
    }
    // 如果当前的url为空，吧传过来的url替换为当前的url
    if (self.currentUrlStr == nil) {
        self.currentUrlStr = urlStr;
    }
    
    self.songItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:urlStr]];
    
    // 如果当前的url ！= 要播放的url， 替换url
    if (self.player.currentItem == nil || ![self.currentUrlStr isEqualToString:urlStr]) {
        
        [self.player replaceCurrentItemWithPlayerItem:self.songItem];
        self.currentUrlStr = urlStr;
    }
}

// 播放
- (void)playSong
{
    [self.player play];
    // 设置正在播放音乐
    self.isPlay = YES;
    
    if (self.timer) {   //如果播放器不为空， return
        return;
    } else {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(playAction:) userInfo:nil repeats:YES];
    }
}
// 暂停
- (void)pauseSong
{
    if (!self.isPlay) {
        return;
    } else {
        [self.player pause];
        self.isPlay = NO;
        [self.timer invalidate];
        self.timer = nil;
    }
}
- (void)stopSong
{
    if (!self.isPlay) {
        return;
    } else {
        [self.player pause];
        self.isPlay = NO;
    }
}

- (void)sliderToTimer:(float)time
{
    [self pauseSong];
    [self.player seekToTime:CMTimeMakeWithSeconds(time, self.player.currentTime.timescale) completionHandler:^(BOOL finished) {
        if (finished) {
            self.isPlay = YES;
            [self playSong];
        }
    }];
    
}

- (float)during
{
    return CMTimeGetSeconds(self.player.currentItem.duration);
}


@end
